package com.kzy.demoadmin.web.config;

import com.kzy.demoadmin.web.utils.JwtInterceptor;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;



/**
 * 拦截器配置
 */
@Configuration
@Slf4j
//@RestController
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private JwtInterceptor jwtInterceptor;


    /**
     * 设置静态资源映射
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

    }

    /**
     * 为所有带@RestController注解的接口添加拦截器
     *
     * @param configurer
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        // 指定controller统一的接口前缀，统一接口前缀,拼接url，方便管理
        configurer.addPathPrefix("api", clazz -> clazz.isAnnotationPresent(RestController.class)&&
                clazz.getPackage().getName().startsWith("com.kzy.demoadmin")
    );
    }

    /**
     * 自定义拦截器JwtInterceptor,设置拦截规则
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加拦截器,除了登录、注册接口，其他接口都需要token验证
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/api/**")
                .excludePathPatterns("/api/users/login")
                .excludePathPatterns("/api/users/register")
                .excludePathPatterns("/api/users/verify-code/**")
                .excludePathPatterns("/api/files/**")
                .excludePathPatterns("/api/bookTypes/export")
                .excludePathPatterns("/api/bookTypes/import")
                .excludePathPatterns("/api/doc.html",
                        "/api/webjars/**",
                        "/api/v3/api-docs/**",
                        "/api/knife4j/**");
    }
}